package com.example.demo.cdesign.service;

import com.example.demo.cdesign.entity.Unit;
import com.example.demo.common.BaseService;
import com.example.demo.cdesign.dao.HwrPowerForecastDao;
import com.example.demo.cdesign.entity.HwrPowerForecast;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.UUID;

@Service
@Transactional(readOnly=true)
public class HwrPowerForecastService extends BaseService<HwrPowerForecast, HwrPowerForecastDao> {

    public void delete(HwrPowerForecast hwrPowerForecast) {
        dao.delete(hwrPowerForecast);
    }

    public void save(HwrPowerForecast hwrPowerForecast) {
        if (hwrPowerForecast.getId() != null) {
            dao.update(hwrPowerForecast);
        } else {
            preInsert(hwrPowerForecast);
            dao.insert(hwrPowerForecast);
        }
    }

    public String generateTaskId() {
        return DateFormatUtils.format(new Date(), "yyyyMMdd");
    }

    private void preInsert(HwrPowerForecast hwrPowerForecast) {
        hwrPowerForecast.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        hwrPowerForecast.setTaskId(generateTaskId());
        hwrPowerForecast.setCodes("");
        hwrPowerForecast.setUnit(new Unit("testId"));
        hwrPowerForecast.setCreateDate(new Date());
    }

}
